package com.baidu.user.service;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baidu.pojo.User;
import com.baidu.user.mapper.UserMapper;
import com.monliao.util.HttpUtil;
import com.monliao.util.Md5Util;

import net.sf.json.JSONObject;

@Service
public class OauthService {

		
		@Autowired
		private UserMapper mapper;

		public User dooauthMsg(String code,  HttpServletRequest request) {
			
		
			//第三方
			System.out.println(code);
			String url="https://openapi.baidu.com/social/oauth/2.0/token";
			Map<String, String> paramsMap=new HashMap<String, String>();
			paramsMap.put("grant_type", "authorization_code");
			paramsMap.put("client_id", "GrS67GUaVSUxnwoLlZds0Yfg");
			paramsMap.put("client_secret", "lOAdnudPYXH0L3r7qRE764d8DZHhC7Qz");
			paramsMap.put("redirect_uri", "http://127.0.0.1/oauthMsg.action");
			paramsMap.put("code", code);
			String json = HttpUtil.post(url, paramsMap);
			System.out.println(json);
			//获取数据
			paramsMap.clear();
			String access_token = JSONObject.fromObject(json).get("access_token").toString();
			paramsMap.put("access_token", access_token);
			url="https://openapi.baidu.com/social/api/2.0/user/info";
			String msg = HttpUtil.post(url, paramsMap);
			System.out.println(msg);		
			String  headurl = JSONObject.fromObject(msg).get("tinyurl").toString();
			 String username = JSONObject.fromObject(msg).get("username").toString();
			 String social_uid = JSONObject.fromObject(msg).get("social_uid").toString();
			 
			System.out.println(headurl);
			
			
		
			
			//判断是否绑定
			paramsMap.clear();
			paramsMap.put("access_token", access_token);
			url="https://openapi.baidu.com/social/api/2.0/user/bind_status";
			String bind_status = HttpUtil.post(url, paramsMap);
			
			System.out.println("判断是否绑定"+bind_status);
			boolean key = JSONObject.fromObject(bind_status).containsKey("media_type");
			if (key) {
				System.out.println("已经绑定的用户");
				//unbind.Unbind(access_token);
			}else{
				System.out.println("未绑定的用户");
				//绑定第三方平台用户
				paramsMap.clear();
				 String media_uid =  JSONObject.fromObject(msg).get("media_uid").toString();

				paramsMap.put("access_token", access_token);
				paramsMap.put("uid", media_uid);
				String uid_sign=media_uid+"lOAdnudPYXH0L3r7qRE764d8DZHhC7Qz";
				paramsMap.put("uid_sign", Md5Util.getMD5(uid_sign));
				
				url="https://openapi.baidu.com/social/api/2.0/user/bind";
				String bindmsg = HttpUtil.post(url, paramsMap);
				System.out.println("绑定第三方平台用户"+bindmsg);
				
				User user=new User();
				String id=System.currentTimeMillis()+"";
				user.setId(id);
				user.setCardurl(headurl);
				user.setNickname(username);
				user.setSocialUid(social_uid);
				mapper.insertSelective(user);
			}
			User logininfo=mapper.selectBySocial_uid(social_uid);
			System.out.println(logininfo.toString());
			return logininfo;
		}

			
		
			

			
		
	
		
		
}
